const app = getApp();
const http = require('../../../utils/util.js');
const mta = require('../../../mta_sdk/mta_analysis.js');
Page({

  data: {
    modelList:[

        {
          name: "XR",
          color: ["XR_黄", "XR_白", "XR_黑","XR_黑","XR_珊瑚"],
          rom: ["64GB", "256GB", "512GB"],
          price: [6499, 6999, 7899]
        },
        {
          name:"XS_MAX",
          color: ["XS_MAX_灰", "XS_MAX_金", "XS_MAX_银"],
          rom: ["64GB", "256GB", "512GB"],
          price: [9599, 10999, 12799]
        },
        {
          name: "XS",
          color: ["XS_灰", "XS_金", "XS_银"],
          rom: ["64GB", "256GB", "512GB"],
          price: [8699, 10099, 11899]
        }

    ],
    region: [],
    pid:'',
    address:'',
    userName:'',
    showColor: false,
    showRom: false,
    lastColor:'',
    lastRom:'',
    lastPrice: '',
    actName:'',
    romIndex:'',
    date:'',
    status:'',
    showStatus:false
  },
  onShow() {
    var cover = wx.getStorageSync('coverpic')
    if (cover || cover != '') {
      this.setData({
        lastImg: cover || '',
        stuPic: cover || ''
      })
    }
  },
  onLoad: function (opt) {
    mta.Page.init();
    console.log(opt);
    if (opt.pid) {
      this.setData({
        baseImg: `/images/${opt.pid}_list.jpg`,
        bgpic: `/images/${opt.pid}_canvas.jpg`,
        pid: opt.pid
      })
    }

  },
  bindRegionChange(e){
    console.log('picker发送选择改变，携带值为', e.detail.value)
    let d=e.detail.value;
    this.setData({
      region:d,
      address:d[0]+d[1]+d[2]
    })
  },
  bindDateChange: function(e) {
    console.log('date', e.detail.value)
    this.setData({
      date: e.detail.value
    })
  },
  formSubmit: function (e) {
    app.sendForm(e);
  },
  getAddress: function (e) {
    this.setData({
      address: e.detail.value
    })
  },
  getStatus: function (e) {
    this.setData({
      status: e.detail.value
    })
  },
  getName: function (e) {
    this.setData({
      userName: e.detail.value
    })
  },
  selesctStatus: function (e) {
    this.setData({
      status: e.currentTarget.dataset.status,
      showStatus:false
    })
  },
  showstatus(){
    this.setData({
      showStatus:true
    })
  },
  showcolor(){
    this.setData({
      showColor:true
    })
  },
  showrom() {
    if (this.data.lastColor == '') {
      this.tip("请先选择型号");
      return false;
    }
    this.setData({
      showRom: true
    })
  },
  selesctColor(e){
    let dat = e.currentTarget.dataset;
    let color = dat.color;
    let name = dat.name;
    let idx=dat.index
    this.data.modelList.forEach((item, index) => {
      if (item.name == name && this.data.romIndex) {
        console.log(item.price[this.data.romIndex])
          this.setData({
            lastPrice: item.price[this.data.romIndex]
          });
      }
    })
    this.setData({
      showColor: false,
      lastColor: color,
      actName: name
    })
  },
  selesctRom(e) {
    let dat = e.currentTarget.dataset;
    let rom = dat.rom;
    let idx = dat.index
    console.log(rom)
    //遍历匹配价格
    
    this.data.modelList.forEach((item, index) => {
      if (item.name == this.data.actName) {
        console.log(item.price[idx],idx)
          this.setData({
            lastPrice: item.price[idx]
          });
      }
    })
    this.setData({
      showRom: false,
      lastRom: rom,
      romIndex:idx
    });
  },

  toShowImg() {
    var md = this.data;
    if (md.userName == '') {
      this.tip("请输入名字")
    }
    else if (md.address == '') {
      this.tip("请输入地址")
    }
    else if (md.lastColor == '') {
      this.tip("请选择型号")
    }
    else if (md.lastRom == '') {
      this.tip("请选择容量")
    }
    else if (md.date == '') {
      this.tip("请选择容量")
    }
    else {
      wx.navigateTo({
        url: `/pages/${md.pid}/${md.pid}_detail/${md.pid}_detail?userName=${md.userName}&address=${md.address}&lastColor=${md.lastColor}&lastRom=${md.lastRom}&lastPrice=${md.lastPrice}&date=${md.date}&status=${md.status}&pid=${this.data.pid}`,
      })
    }

  },
  tip(txt) {
    wx.showToast({
      title: txt,
      icon: 'none'
    });
  },
  onUnload() {
    wx.removeStorageSync("coverpic")
  },
  onShareAppMessage: function () {
    return {
      title: '新iphone订单生成',
      desc: '新iphone订单生成',
      path: `/pages/index/index`,

    }
  }
})